package com.example.spindrift.service;

import com.example.spindrift.dao.SysSongMapper;
import com.example.spindrift.models.SysSong;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SongService {
    @Resource
    SysSongMapper sysSongMapper;

    @Autowired
    SingService singService;
    @Autowired
    FileService fileService;

    /**
     * 获取所有歌曲信息
     * @return
     */
    public List<SysSong> getAllSong(){
        return sysSongMapper.getAllSong();
    }

    public List<SysSong> getSongByType(Integer typeId) {
        return sysSongMapper.getSongByType(typeId);
    }

    public List<SysSong> getSongByName(String songName) {
        return sysSongMapper.getSongByName(songName);
    }

    public List<SysSong> searchSong(String keyword) {
        return sysSongMapper.searchSong(keyword);
    }

    public List<SysSong> getNewSong(){
        return sysSongMapper.getNewSong();
    }
    /**
     * 根据用户ID获取指定用户上传的音乐信息
     * @param userId 用户ID
     * @return
     */
    public List<SysSong> getSongByUser(Integer userId){
        return sysSongMapper.getSongByUser(userId);
    }

    public SysSong getSongById(Integer songId) {
        return sysSongMapper.getSongById(songId);
    }

    public List<SysSong> getSongBySing(Integer singId) {
        return sysSongMapper.getSongBySing(singId);
    }
    public List<SysSong> getHostRank(){
        return sysSongMapper.getHostRank();
    }
    public List<SysSong> getCollectRank(){
        return sysSongMapper.getCollectRank();
    }

    public Integer insertSong(SysSong song) {
        return sysSongMapper.insertSong(song);
    }

    public Integer updateSong(SysSong song) {
        return sysSongMapper.updateSong(song);
    }

    public Integer updateSongState(Integer songId, String state) {
        return sysSongMapper.updateSongState(songId,state);
    }

    public Integer deleteSong(Integer songId) {
        return sysSongMapper.deleteSong(songId);
    }

    public Integer updateSongHost(Integer songId,Integer host){
        return sysSongMapper.updateSongHost(songId,host);
    }

//    public SysSong uploadMusic (MultipartFile music){
//        SysFile musicFile;
//        SysFile imageFile = null;
//        File mus = null;
//        try {
//            mus = File.createTempFile("lsFile",".mp3");
//            music.transferTo(mus);
//            // 解析mp3歌曲内容
//            MP3Analysis.Mp3Mate mate = MP3Analysis.getMP3Mate(mus);
//            // 保存专辑图片
//            if(mate.getAlbumImage() != null && mate.getAlbumImage().length > 0){
//                imageFile = fileService.saveFile(mate.getAlbumImage(),".jpeg");
//            }
//            // 保存歌曲文件
//            musicFile = fileService.saveFile(mus);
//
//            // 获取/添加获取歌手信息
//            List<SysSing> singList = singService.getSingByName(mate.getArtist());
//            SysSing sing = new SysSing();
//            if(singList.size() > 0 && singList.get(0) != null){
//                sing = singList.get(0);
//            }else{
//                sing.sing_name = mate.getArtist();
//                if(singService.insertSing(sing) > 0){
//                    sing = singService.getSingByName(mate.getArtist()).get(0);
//                }
//            }
//            SysSong song = new SysSong();
//            song.name = mate.getTitle();
//            song.file_id = musicFile.file_id;
//            song.sing_id = sing.sing_id;
//            song.type_id = mate.getGenre();
//            song.long_time = Math.toIntExact(mate.getLengthInSeconds()); // 歌曲时长（秒）
//            if(imageFile != null)
//                song.album_img = imageFile.file_id;
//
//            if(sysSongMapper.insertSong(song) > 0){
//                return sysSongMapper.getSongByName(song.name).get(0);
//            }
//        } catch (IOException | InvalidDataException | UnsupportedTagException e) {
//            e.printStackTrace();
//        } finally {
//            if(mus != null){
//                mus.deleteOnExit();
//            }
//        }
//        return null;
//    }
    public class MusicBody{
        MultipartFile music;
        MultipartFile lrc;

        public MultipartFile getMusic() {
            return music;
        }

        public void setMusic(MultipartFile music) {
            this.music = music;
        }

        public MultipartFile getLrc() {
            return lrc;
        }

        public void setLrc(MultipartFile lrc) {
            this.lrc = lrc;
        }
    }
}
